package terminal.ui;

import java.awt.Dimension;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;

public class PriceChart extends JPanel {

    private JFreeChart chart;
    private ChartPanel chartPanel;
    private TimeSeries series;
    private TimeSeriesCollection dataset;

    public PriceChart(String title) {
        series = new TimeSeries("Price", Millisecond.class);
        dataset = new TimeSeriesCollection(series);
        createChart(title, dataset);
        chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(530, 250));
        add(chartPanel);
    }

    private void createChart(String title, XYDataset dataset) {
        chart = ChartFactory.createTimeSeriesChart("Price of " + title, "Time", "Price", dataset, true, true, false);
        XYPlot plot = chart.getXYPlot();
        ValueAxis axis = plot.getDomainAxis();
        axis.setAutoRange(true);
        axis.setFixedAutoRange(60000.0);
        axis = plot.getRangeAxis();
        axis.setRange(0.0, 60000.0);
    }

    public void addData(double price) {
        series.add(new Millisecond(), price);
    }
}
